/* app_fir_convolution_example */

#include "app.h"
#include "app_fir_data.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*******************************************************************************
 * Variables
 ******************************************************************************/

/*******************************************************************************
 * Prototypes
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/

void app_arm_correlation_f32_example(void)
{
    PRINTF("%s()\r\n", __func__);

    /* 清零输出队列 */
    memset(fir_output_y_f32, 0, sizeof(float32_t) * FIR_CONV_OUTPUT_NUM );

    TimerCount_Start();
    arm_correlate_f32(   fir_input_x_f32, FIR_INPUT_NUM,
                         fir_coeff_b_f32_reversed, FIR_COEFF_NUM,
                         fir_output_y_f32 );
    TimerCount_Stop(tick_counter_val);

    /* 打印最终结果. */
    for (uint32_t i = 0; i < FIR_CONV_OUTPUT_NUM; i++)
    {
        PRINTF("[%4d]: %f\r\n", i, fir_output_y_f32[i]);
    }
    PRINTF("done with %d us.\r\n\r\n", tick_counter_val/96u);
}

void app_arm_correlation_f32_example_2(void)
{
    PRINTF("%s()\r\n", __func__);

    /* 清零输出队列 */
    memset(fir_output_y_f32, 0, sizeof(float32_t) * FIR_CONV_OUTPUT_NUM );

    TimerCount_Start();
    arm_correlate_f32(   fir_coeff_b_f32_reversed, FIR_COEFF_NUM,
                         fir_input_x_f32, FIR_INPUT_NUM,
                         fir_output_y_f32 );
    TimerCount_Stop(tick_counter_val);

    /* 打印最终结果. */
    for (uint32_t i = 0; i < FIR_CONV_OUTPUT_NUM; i++)
    {
        PRINTF("[%4d]: %f\r\n", i, fir_output_y_f32[i]);
    }
    PRINTF("done with %d us.\r\n\r\n", tick_counter_val/96u);
}


/* 重新封装的使用PQ计算相关性的驱动函数. */
void PQ_Correlation(POWERQUAD_Type *base,
       void *pAData, int32_t ALength,
       void *pBData, int32_t BLength,
       void *pResult)
{
    PQ_FIR(base, pAData, ALength, pBData, BLength, pResult, PQ_FIR_CORRELATION);
}

void app_pq_correlation_f32_example(void)
{
    PRINTF("%s()\r\n", __func__);

    /* 清零输出队列 */
    memset(fir_output_y_f32, 0, sizeof(float32_t) * FIR_CONV_OUTPUT_NUM );

    /* 设定浮点数计算. */
    PQ_SetFormat(POWERQUAD, kPQ_CP_FIR, kPQ_Float);

    /* 计算相关性. */
    TimerCount_Start();
    PQ_Correlation(POWERQUAD,
        fir_input_x_f32, FIR_INPUT_NUM,
        fir_coeff_b_f32_reversed, FIR_COEFF_NUM,
        fir_output_y_f32);
    PQ_WaitDone(POWERQUAD);
    TimerCount_Stop(tick_counter_val);

    /* 打印最终结果. */
    for (uint32_t i = 0; i < FIR_CONV_OUTPUT_NUM; i++)
    {
        PRINTF("[%4d]: %f\r\n", i, fir_output_y_f32[i]);
    }
    PRINTF("done with %d us.\r\n\r\n", tick_counter_val/96u);
}

/* 基于app_pq_correlation_f32_example()
 * x[n]和b[m]交换位置算一下, 希望得到的结果序列也是倒序的.
 */
void app_pq_correlation_f32_example_2(void)
{
    PRINTF("%s()\r\n", __func__);

    /* 清零输出队列 */
    memset(fir_output_y_f32, 0, sizeof(float32_t) * FIR_CONV_OUTPUT_NUM );

    /* 设定浮点数计算. */
    PQ_SetFormat(POWERQUAD, kPQ_CP_FIR, kPQ_Float);

    /* 计算相关性. */
    TimerCount_Start();
    PQ_Correlation(POWERQUAD,
        fir_coeff_b_f32_reversed, FIR_COEFF_NUM,
        fir_input_x_f32, FIR_INPUT_NUM,
        fir_output_y_f32);
    PQ_WaitDone(POWERQUAD);
    TimerCount_Stop(tick_counter_val);

    /* 打印最终结果. */
    for (uint32_t i = 0; i < FIR_CONV_OUTPUT_NUM; i++)
    {
        PRINTF("[%4d]: %f\r\n", i, fir_output_y_f32[i]);
    }
    PRINTF("done with %d us.\r\n\r\n", tick_counter_val/96u);
}

/* EOF. */

